/* 
    Squirrel open project
    Copyright (C) 2012  Elias Aran Paul Farhan
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#include <squirrel/character.hpp>
Character::Character(std::string filename, Point pos, Point size):GameObject(filename, pos, size)
{
	movement = new Animation();
	count = 0;
	currentImg = 0;
}
Character::~Character()
{
	delete movement;
}
void Character::setState(int state)
{
	movement->setState(state);
}
std::vector<Box*>* Character::getBoxes()
{
	return movement->getBoxes();
}
void Character::updateSpeed()
{
	speed = movement->getSpeed();
}
void Character::update(std::vector<Box*> nearbox)
{
	updateSpeed();
	if (count == 0)
	{
		nextImage();
	}
	count = (count+1)%4;
	Point tmppos = std::make_pair(speed.first+pos.first, speed.second+pos.second);
	bool collide = false;
	std::vector<Box*> statebox = (*getBoxes());
	for(std::vector<Box*>::iterator it = nearbox.begin(); it != nearbox.end(); it++)
	{
		for(std::vector<Box*>::iterator itbox = statebox.begin(); itbox != statebox.end(); itbox++)
		{
			if((*it)->squareIsIn(std::make_pair((*itbox)->getOrigin().first + tmppos.first, (*itbox)->getOrigin().second + tmppos.second),
								 std::make_pair((*itbox)->getEnd().first + tmppos.first, (*itbox)->getEnd().second + tmppos.second)))
			{
				collide = true;
				break;
			}
		}
	}
	if(!collide)
	{
		pos = tmppos;
	}
	boxes[0]->setPosition(pos, std::make_pair(pos.first+size.first, pos.second+size.second));
}
void Character::setDirection(bool keys[4])
{
	movement->setDirection(keys);
}
void Character::addState(const char* name, int speed, int frames[4], int index)
{
	movement->addState(name, speed, frames, index);
}
void Character::addBox(Box* b, int state)
{
	movement->addBox(b, state);
}
void Character::addBox(Box* b)
{
	boxes.push_back(b);
}
void Character::nextImage()
{
	currentImg = movement->next();
	image = imgs[currentImg];
	
}
void Character::setImage(std::vector<GLuint> imgs)
{
	this->imgs = imgs;
}
Box* Character::getCheckBox()
{
	return boxes[0];
}
